package com.bbird.od.finance.controller;

import com.bbird.od.finance.service.models.type.BankType;
import com.bbird.od.finance.service.domain.ReconciliationHandlerFactory;
import com.bbird.od.finance.service.domain.ReconciliationService;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.paint.Paint;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;

/**
 * @author ziyang Kuang
 * @date 2021/05/01
 * @describe
 */

@Slf4j
@Data
public class ReconciliationDomain {
    private BankType bankType;
    private ProgressBar progressBar;
    private Label processLB;
    private boolean finished = false;

    private StringProperty erpFlow = new SimpleStringProperty();
    private StringProperty bankFlow = new SimpleStringProperty();
    private StringProperty deliveryFlow = new SimpleStringProperty();
    private StringProperty historyFlow = new SimpleStringProperty();

    public ReconciliationDomain(BankType bankType) {
        this.bankType = bankType;
    }

    public String checkParam() {
        if (StringUtils.isBlank(erpFlow.getValue())) {
            return bankType.getLabel() + "ERP流水文件不能为空";
        } else if (!new File(erpFlow.getValue()).exists()) {
            return bankType.getLabel() + "ERP流水文件打开失败";
        }

        if (StringUtils.isBlank(bankFlow.getValue())) {
            return bankType.getLabel() + "银行流水文件不能为空";
        } else if (!new File(bankFlow.getValue()).exists()) {
            return bankType.getLabel() + "银行流水文件打开失败";
        }

        if (StringUtils.isBlank(deliveryFlow.getValue())) {
            return bankType.getLabel() + "发货流水文件不能为空";
        } else if (!new File(deliveryFlow.getValue()).exists()) {
            return bankType.getLabel() + "发货流水文件打开失败";
        }
        if (StringUtils.isNotBlank(historyFlow.getValue())
                && !new File(historyFlow.getValue()).exists()) {
            return bankType.getLabel() + "历史记录文件打开失败";
        }

        return null;
    }

    public void start(String savePath) {
        log.info("[{}]对账开始.......", this.bankType.getLabel());

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                // 初始化界面
                progressBar.setProgress(0);
                processLB.setText("");
                finished = false;
            }
        });

        // 开始对账
        ReconciliationService reconciliationService = ReconciliationHandlerFactory.create(this.bankType);
        File erpFile = new File(this.erpFlow.getValue());
        File bankFile = new File(this.bankFlow.getValue());
        File deliveryFile = new File(this.deliveryFlow.get());
        File historyFile = null;
        if (StringUtils.isNotBlank(this.historyFlow.getValue())) {
            historyFile = new File(this.historyFlow.getValue());
        }
        final File historyFileFinal = historyFile;
        // 异步处理为了显示进度条
        new Thread(() -> reconciliationService.reconciliation(erpFile, bankFile, deliveryFile, historyFileFinal, savePath)).start();

        // 进度条
        while (!reconciliationService.isFinished()) {
            progressBar.setProgress(reconciliationService.getCurrentProgress().doubleValue());
        }

        progressBar.setProgress(1d);
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                if (reconciliationService.isSuccess()) {

                    processLB.setText("对账成功");
                } else {
                    processLB.setText("对账失败");
                    processLB.setTextFill(Paint.valueOf("Red"));
                }
            }
        });

        finished = true;
    }
}
